'''
Description: 
Author: 爱酱丶
Date: 2022-09-02 11:30:13
LastEditTime: 2022-09-07 17:00:58
LastEditors: 爱酱丶
'''
import flask,json
from flask import Flask, jsonify,request
from gevent import pywsgi
from ESA_download_API import sentinel2_api


# 创建一个服务
app = Flask(__name__)
app.config.from_object(__name__)
app.config["JSON_AS_ASCII"] = False


# 注册蓝图
app.register_blueprint(sentinel2_api)


@app.route('/index',methods=['get','post'])
def index():
    ren = {'msg':'成功访问首页','msg_code':200}
    #json.dumps 序列化时对中文默认使用的ascii编码.想输出中文需要指定ensure_ascii=False
    # return json.dumps(ren,ensure_ascii=False)
    # l = [1,2,2,2,2,2]
    return jsonify(ren)


#post入参访问方式一：url格式参数
@app.route('/article',methods=['post']) 
def article():
    #url格式参数?id=12589&name='lishi'
    id = flask.request.args.get('id')

    if id:
        if id == '12589':
            ren = {'msg':'成功访问文章','msg_code':200}
        else:
            ren = {'msg':'找不到文章','msg_code':400}
    else:
        ren = {'msg':'请输入文章id参数','msg_code':-1}
    return json.dumps(ren,ensure_ascii=False)
 
 
#post入参访问方式二：from-data（k-v）格式参数
@app.route('/login',methods=['post'])
def login():
    #from-data格式参数
    usrname = flask.request.values.get('usrname')
    pwd = flask.request.values.get('pwd')

    if usrname and pwd:
        if usrname =='test' and pwd =='123456':
            ren = {'msg':'登录成功','msg_code':200}
        else:
            ren = {'msg':'用户名或密码错误','msg_code':-1}
    else:
        ren = {'msg':'用户名或密码为空','msg_code':1001}
    return json.dumps(ren,ensure_ascii=False)


#post入参访问方式二：josn格式参数  
@app.route('/loginjosn',methods=['post'])
def loginjosn():
    #from-data格式参数
    usrname = flask.request.json.get('usrname')
    pwd = flask.request.json.get('pwd')

    if usrname and pwd:
        if usrname =='test' and pwd =='123456':
            ren = {'msg':'登录成功','msg_code':200}
        else:
            ren = {'msg':'用户名或密码错误','msg_code':-1}
    else:
        ren = {'msg':'用户名或密码为空','msg_code':1001}
    return json.dumps(ren,ensure_ascii=False)
 
 
if __name__ == '__main__':
    # # 启动服务 指定主机和端口
    # app.debug = True
    # server = pywsgi.WSGIServer(('127.0.0.1', 8888),app)

    # print('server is running...')
    # server.serve_forever()
    app.run(threaded=True,debug=True,host='127.0.0.1',port=8888) # 启动服务
#   # debug=True,改了代码后，不用重启，它会自动重启
#   # 'host='127.0.0.1'别IP访问地址
